package com.yksj.healthtalk.ui.home;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.GroupInfoEntity;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.services.CoreService;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.salon.SalonSearchMainActivity;
/**
 * 我的话题
 * @author jack_tang
 *
 */
public class SalonListActivity extends BaseFragmentActivity implements
		OnClickListener {

	public final String TYPE_NAME = "2";
	public final String TYPE_ATT = "1";
	public String expand_group_text;// 该组的组名
	private EditText searchEdit;
	private Button searchBtn;
	private View headerView;
	private	GroupInfoEntity  mGroupInfoEntity;
	private String flagPlacing;
	private String Flag;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
		setContentView(R.layout.pull_main_friend_group);
		headerView = LayoutInflater.from(this).inflate(R.layout.pull_to_refresh_header_edit, null);
		setUpView();
		// setData();
		
		Fragment fragment = new MySalonListFragment();
		FragmentManager manager = getSupportFragmentManager();
		FragmentTransaction transaction = manager.beginTransaction();
		transaction.replace(R.id.frameLayout, fragment);
		transaction.commit();
	}
	@Override
	protected void onStart() {
		super.onStart();
		Intent intent =new Intent(this,CoreService.class);
		intent.setAction(CoreService.ACTION_FRAME_VIEW_VISIABLE);
		intent.putExtra("className",this.getClass().getName());
		startService(intent);
	}
	private void setUpView() {
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleRightBtn2.setOnClickListener(this);
		titleTextV.setText("我的话题");
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText("搜索");
		searchEdit = (EditText) headerView.findViewById(R.id.refresh_header_edit);
		searchBtn = (Button) headerView.findViewById(R.id.refresh_header_btn);
		searchBtn.setOnClickListener(this);
		searchEdit.setHint(R.string.searchGroup);
		
		
		Flag = "";
		flagPlacing = "-100000";
		mGroupInfoEntity = new GroupInfoEntity();
		mGroupInfoEntity.setFlag(Flag);
		mGroupInfoEntity.setFlagPlacing(flagPlacing);
		mGroupInfoEntity.setPagesize(1);
		mGroupInfoEntity.setType(1);
		mGroupInfoEntity.setCreateCustomerID(SmartFoxClient.getLoginUserId());
	}




	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_ok:
			break;
		case R.id.title_right2:
			Intent 	intent = new Intent();
			intent.setClass(getApplicationContext(), SalonSearchMainActivity.class);
			intent.putExtra("mGroupInfoEntity", mGroupInfoEntity);
			startActivityForResult(intent, 0);
			break;

		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// 搜索结果
		if (resultCode == 0 && requestCode == 0 && data != null) {
			flagPlacing = "-100000";
			Flag = "";
			if (data.hasExtra("positions")) {
				ArrayList<String> list = (ArrayList<String>) data.getExtras().get("positions");
				mGroupInfoEntity = (GroupInfoEntity) data.getExtras().get("mGroupInfoEntity");
				String dataClickId = (String) data.getExtras().get("dataClickId");
				// 不是从搜索首页全部搜索
				if (!dataClickId.equals("0")) {
					mGroupInfoEntity.setFlag(Flag);
					mGroupInfoEntity.setFlagPlacing(flagPlacing);
					mGroupInfoEntity.setType(1);
					mGroupInfoEntity.setUpperId(dataClickId);
					StringBuilder builder = new StringBuilder();
					for (int i = 0; i < list.size(); i++) {
						builder.append(Integer.valueOf(list.get(i)));
						builder.append(",");
					}
					builder.deleteCharAt(builder.toString().length() - 1);
					mGroupInfoEntity.setInfoId(builder.toString());
				} else {
					mGroupInfoEntity.setFlag(Flag);
					mGroupInfoEntity.setFlagPlacing(flagPlacing);
					mGroupInfoEntity.setType(1);
					mGroupInfoEntity.setUpperId(dataClickId);
					mGroupInfoEntity.setInfoId("0");
				}
			}
			Intent intent = new Intent(getApplicationContext(), MySalonSearchActivity.class);
			intent.putExtra("mGroupInfoEntity", mGroupInfoEntity);
			startActivity(intent);
			finish();
	}
	}

}
